~ chicken-core (chicken-5) /manual/Module (chicken file)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken file)
5
6This module provides various generic operations on files and
7directories. For more specific operations, see also
8[[Module (chicken file posix)]].
9
10All errors related to failing file-operations will signal a condition
11of kind {{(exn i/o file)}}.
12
13* New in CHICKEN 5.4.0: Errors caused by underlying C calls that
14 change errno will produce a condition object with an {{errno}}
15 property, which can be accessed with
16 {{(get-condition-property <the-condition-object> 'exn 'errno)}}.
17
18=== Basic file operations
19
20==== create-directory
21
22<procedure>(create-directory NAME #!optional PARENTS?)</procedure>
23
24Creates a directory with the pathname {{NAME}}. If the {{PARENTS?}} argument
25is given and not false, any nonexistent parent directories are also created.
26
27Notice that if {{NAME}} exists, {{create-directory}} won't try to create it
28and will return {{NAME}} (i.e., it won't raise an error when given a {{NAME}}
29that already exists).
30
31==== copy-file
32
33<procedure>(copy-file ORIGFILE NEWFILE #!optional CLOBBER BLOCKSIZE)</procedure>
34
35Copies {{ORIGFILE}} (a string denoting some filename) to {{NEWFILE}},
36{{BLOCKSIZE}} bytes at a time. {{BLOCKSIZE}} defaults to 1024, and
37must be a positive integer. Returns the number of bytes copied on
38success, or errors on failure. {{CLOBBER}} determines the behaviour
39of {{copy-file}} when {{NEWFILE}} is already extant. When set to
40{{#f}} (default), an error is signaled. When set to any other value,
41{{NEWFILE}} is overwritten. {{copy-file}} will work across
42filesystems and devices and is not platform-dependent.
43
44==== move-file
45
46<procedure>(move-file ORIGFILE NEWFILE #!optional CLOBBER BLOCKSIZE)</procedure>
47
48Moves {{ORIGFILE}} (a string denoting some filename) to {{NEWFILE}},
49with the same semantics as {{copy-file}}, above. {{move-file}} is
50safe across filesystems and devices (unlike {{rename-file}}). It is
51possible for an error to be signaled despite partial success if
52{{NEWFILE}} could be created and fully written but removing
53{{ORIGFILE}} fails.
54
55If {{CLOBBER}} is given and not {{#f}}, {{NEWFILE}} will be replaced
56when it already exists, otherwise an error is signaled.
57
58The {{BLOCKSIZE}} argument indicates the block size to use when
59copying the file a block at a time. It must be a positive integer,
60and it defaults to 1024.
61
62==== delete-file
63
64<procedure>(delete-file STRING)</procedure>
65
66Deletes the file with the pathname {{STRING}}. If the file does
67not exist, an error is signaled.
68
69==== delete-file*
70
71<procedure>(delete-file* STRING)</procedure>
72
73If the file with pathname {{STRING}} exists, it is deleted and {{#t}}
74is returned. If the file does not exist, nothing happens and {{#f}}
75is returned.
76
77==== delete-directory
78
79<procedure>(delete-directory NAME [RECURSIVE])</procedure>
80
81Deletes the directory with the pathname {{NAME}}. If {{RECURSIVE}} is
82not given or false, then the directory has to be empty.
83
84==== directory
85
86<procedure>(directory [PATHNAME [SHOW-DOTFILES?]])</procedure>
87
88Returns a list with all files that are contained in the directory with the name {{PATHNAME}}
89(which defaults to the value of {{(current-directory)}}).
90Files beginning with {{.}} are included only if {{SHOW-DOTFILES?}} is given and not {{#f}}.
91
92==== directory-exists?
93
94<procedure>(directory-exists? STRING)</procedure>
95
96Returns {{STRING}} if a directory with the given pathname exists, or
97{{#f}} otherwise.
98
99
100==== file-exists?
101
102<procedure>(file-exists? STRING)</procedure>
103
104Returns {{STRING}} if a file or directory with the given pathname exists, or
105{{#f}} otherwise.
106
107
108==== rename-file
109
110<procedure>(rename-file OLD NEW #!optional CLOBBER)</procedure>
111
112Renames the file or directory with the pathname {{OLD}} to
113{{NEW}}. If the operation does not succeed, an error is signaled.
114
115If {{CLOBBER}} is given and not {{#f}}, {{NEW}} will be replaced when
116it already exists, otherwise an error is signaled.
117
118==== file-readable?
119==== file-writable?
120==== file-executable?
121
122<procedure>(file-readable? FILENAME)</procedure><br>
123<procedure>(file-writable? FILENAME)</procedure><br>
124<procedure>(file-executable? FILENAME)</procedure>
125
126These procedures return {{#t}} if the current user has read,
127write or execute permissions on the file named {{FILENAME}}.
128
129
130=== Temporary files and directories
131
132==== create-temporary-file
133
134<procedure>(create-temporary-file [EXTENSION])</procedure>
135
136Creates an empty temporary file and returns its pathname. If
137{{EXTENSION}} is not given, then {{.tmp}} is used. If the environment
138variable {{TMPDIR}}, {{TEMP}} or {{TMP}} is set, then the pathname
139names a file in that directory. If none of the environment variables
140is given, the location of the temporary file defaults to {{/tmp}}.
141
142Note that {{TMPDIR}}, {{TEMP}} and {{TMP}} are checked in this order.
143It means that, for example, if both {{TMPDIR}} and {{TEMP}} are set,
144the value of {{TMPDIR}} will be used.
145
146Changed in CHICKEN 5.4.0: the values of the {{TMPDIR}}, {{TEMP}} and
147{{TMP}} environment variables are no longer memoized (see
148[[#1830|https://bugs.call-cc.org/ticket/1830]]).
149
150==== create-temporary-directory
151
152<procedure>(create-temporary-directory)</procedure>
153
154Creates an empty temporary directory and returns its pathname. If the
155environment variable {{TMPDIR}}, {{TEMP}} or {{TMP}} is set, then the
156temporary directory is created at that location. If none of the
157environment variables is given, the location of the temporary
158directory defaults to {{/tmp}}.
159
160Note that {{TMPDIR}}, {{TEMP}} and {{TMP}} are checked in this order.
161It means that, for example, if both {{TMPDIR}} and {{TEMP}} are set,
162the value of {{TMPDIR}} will be used.
163
164Changed in CHICKEN 5.4.0: the values of the {{TMPDIR}}, {{TEMP}} and
165{{TMP}} environment variables are no longer memoized (see
166[[#1830|https://bugs.call-cc.org/ticket/1830]]).
167
168=== Finding files
169
170==== find-files
171
172<procedure>(find-files DIRECTORY #!key test action seed limit dotfiles follow-symlinks)</procedure>
173
174Recursively traverses the contents of {{DIRECTORY}} (which should be a
175string) and invokes the procedure {{action}} for all files in which
176the procedure {{test}} is true.
177
178{{test}} may be a procedure of one argument or an irregex object,
179regex string or SRE expression that will be matched with a full
180pathname using {{irregex-match}}. {{test}} defaults to {{(constantly
181#t)}}.
182
183
184{{action}} should be a procedure of two arguments: the currently
185encountered file and the result of the previous invocation of
186{{action}}, or, if this is the first invocation, the value of
187{{seed}}. {{action}} defaults to {{cons}}, {{seed}} defaults to {{()}}.
188
189{{limit}} should be a procedure of one argument that is called for
190each nested directory and which should return true, if that directory
191is to be traversed recursively. {{limit}} may also be an exact integer
192that gives the maximum recursion depth. For example, a depth of {{0}}
193means that only files in the top-level, specified directory are to be
194traversed. In this case, all nested directories are ignored.
195{{limit}} may also be {{#f}} (the default), which is equivalent to
196{{(constantly #t)}}.
197
198If {{dotfiles}} is given and true, then files starting with a "{{.}}"
199character will not be ignored (but note that "{{.}}" and "{{..}}" are
200always ignored). if {{follow-symlinks}} is given and true, then the
201traversal of a symbolic link that points to a directory will
202recursively traverse the latter. By default, symbolic links are not
203followed.
204
205Note that {{action}} is called with the full pathname of each file,
206including the directory prefix.
207
208
209==== glob
210
211<procedure>(glob PATTERN1 ...)</procedure>
212
213Returns a list of the pathnames of all existing files matching
214{{PATTERN1 ...}}, which should be strings containing the usual
215file-patterns (with {{*}} matching zero or more characters and
216{{?}} matching zero or one character).
217
218---
219Previous: [[Module (chicken eval)]]
220
221Next: [[Module (chicken file posix)]]